﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace WebApiTest.Dto.Utils
{
    public class TableReqDTO
    {
        public int pageNo { get; set; }
        public int pageSize { get; set; }
        public string sortField { get; set; }
        public string sortOrder { get; set; }
        public string queryParam { get; set; }

        public TableReqDTO(int pageNo, int pageSize, string sortField, string sortOrder, string queryParam)
        {
            this.pageNo = pageNo;
            this.pageSize = pageSize;
            this.sortField = sortField;
            this.sortOrder = sortOrder;
            this.queryParam = queryParam;
        }

        public int getStart()
        {
            return (this.pageNo - 1) * this.pageSize;
        }


        public String parseQueryParam(String paramName)
        {
            String queryValue = null;
            JObject jsonObject = JObject.Parse(queryParam);
            if (!"{}".Equals(queryParam) && queryParam != null && jsonObject[paramName] != null)
            {
                queryValue = jsonObject[paramName].ToString();
            }
            return queryValue;
        }

        public String CamelCaseToUnderline(String strItem)
        {
            string strItemTarget = "";  //目标字符串
            for (int j = 0; j < strItem.Length; j++)  //strItem是原始字符串
            {
                string temp = strItem[j].ToString();
                if (Regex.IsMatch(temp, "[A-Z]"))
                {
                    temp = "_" + temp.ToLower();
                }
                strItemTarget += temp;
            }
            return strItemTarget;
        }
    }
}
